//
//  EquipmentSelectViewController.m
//  EPBPro
//
//  Created by wucha on 2018/7/10.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "EquipmentSelectViewController.h"
#import "EquipmentActionInfo.h"
#import "AreaInfo.h"
#import "EquipmentSelectTableViewCell.h"
#import "MachineDetailViewController.h"

@interface EquipmentSelectViewController ()
@property (nonatomic,strong) AreaInfo *areaInfo;
@property (nonatomic,strong) NSString *status;
@property (nonatomic,strong) SelectedBlock selectedBlock;
@property (nonatomic,strong) NSString *orgType;

@end

@implementation EquipmentSelectViewController

-(id)init{
    if (self == [super init]) {
        AreaInfo *areaInfo = [[AreaInfo alloc] init];
        areaInfo.name = @"苏州市";
        areaInfo.code = @"3205";
        self.areaInfo = areaInfo;
        self.status = @"open";
    }
    return self;
}

-(id)initWithOrgType:(NSString*)orgType Block:(SelectedBlock)block{
    self = [self init];
    if (self) {
        self.orgType = orgType;
        self.selectedBlock = block;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleName = @"选择机器(已选择0台)";
    UIButton *rightbutton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
    rightbutton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightbutton setTitle:@"完成" forState:UIControlStateNormal];
    [rightbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightbutton addTarget:self action:@selector(finishAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightbutton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    
    self.tableView.separatorStyle = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadData];
}

-(void)finishAction:(id)sender{
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (EquipmentInfo *equipmentInfo in self.resultArray) {
        if (equipmentInfo.isClick) {
            [mutableArray addObject:equipmentInfo.id];
        }
    }
    if (mutableArray.count == 0) {
        [self showMessage:@"请选择机器" autoDissmissAfterDelay:0];
    }
    
    if (self.selectedBlock) {
        self.selectedBlock(mutableArray);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)loadData{
    __weak EquipmentSelectViewController *filterView = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [filterView loadHeader];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
    
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadFooter)];
    
    EquipmentActionInfo *equipmentActionInfo = [[EquipmentActionInfo alloc] init];
//    equipmentActionInfo.status = self.status;
    equipmentActionInfo.city = self.areaInfo.name;
    if ([self.orgType isEqualToString:@"RENT"]) {
        equipmentActionInfo.status = @"open";
    }else if([self.orgType isEqualToString:@"AGENT"]){
        equipmentActionInfo.hasAgent =@"N";

    }else if ([self.orgType isEqualToString:@"CHANNEL"]){
        equipmentActionInfo.hasChannel = @"N";
    }
    [self setClass:@"HttpEquipmentAction" action:@selector(postEquipmentListWithAction:block:) object:equipmentActionInfo];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.resultArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    EquipmentSelectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[EquipmentSelectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    EquipmentInfo *equipmentInfo = self.resultArray[indexPath.row];
    [cell setMachineInfo:equipmentInfo];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    EquipmentInfo *equipmentInfo = self.resultArray[indexPath.row];
    equipmentInfo.isClick = !equipmentInfo.isClick;
    [self.tableView reloadData];
//    MachineDetailViewController *machineDetailViewController = [[MachineDetailViewController alloc] initWithEquipmentInfo:equipmentInfo];
//    [self.navigationController pushViewController:machineDetailViewController animated:YES];
    
    self.titleName = [NSString stringWithFormat:@"选择机器(已选择%d台)",[self getSelectedNumber]];

}

-(int)getSelectedNumber{
    NSMutableArray *mutableArray = [NSMutableArray array];
    for (EquipmentInfo *equipmentInfo in self.resultArray) {
        if (equipmentInfo.isClick) {
            [mutableArray addObject:equipmentInfo.id];
        }
    }
    return (int)mutableArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
